package mainpackage;

//http://en.wikipedia.org/wiki/Cycle_detection#Tortoise_and_hare (Floyd's algorithm it is tortoise and hare)
public class FloydsAlgorithm {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		System.out.println("Our sequence");
		int x0=8;
		int next=x0;
		for(int i=0;i<30;i++){
			System.out.print(next+",");
			next=f(next);
		}
		System.out.println();
		
		
		
		
		floyd( x0);
	}
	
	
	public static void floyd(int x0){
		System.out.println("Floyd's Algorithm");
		
		int mu=0;
		
		
		int tortoize=f(x0);
		int hare=f(f(x0));
		
		while(tortoize!=hare ){
			tortoize=f(tortoize);
			hare=f(f(hare));
		}
		
		tortoize=x0;
		while(tortoize!=hare){
			tortoize=f(tortoize);
			hare=f(hare);
			mu++;
		};
		
		int lam=1;
		hare=f(tortoize);
		while(hare!=tortoize){
			hare=f(hare);
			lam++;
		}
		
		System.out.println("Result : mu="+mu+";"+"lam="+lam);
		System.out.println("Description: mu - is position when cycle begins , lam - is cycle lenght");
	}
	
	public static int f(int x){
		if(x==0) return 6;
		if(x==1) return 6;
		if(x==2) return 0;
		if(x==3) return 1;
		if(x==4) return 4;
		if(x==5) return 3;
		if(x==6) return 3;
		if(x==7) return 4;
		if(x==8) return 0;
		return -1;
	}

}
